//
//  HPersonalizedSignatureVC.m
//  BXlive
//
//  Created by huangzhiwen on 2017/3/22.
//  Copyright © 2017年 cat. All rights reserved.
//

#import "HPersonalizedSignatureVC.h"
#import "BXSAMTextView.h"
#import <FDFullscreenPopGesture/UINavigationController+FDFullscreenPopGesture.h>
#import <SLDeveloperTools/SLDeveloperTools.h>
#import <Masonry/Masonry.h>

@interface HPersonalizedSignatureVC ()<UITextViewDelegate>

@property (nonatomic,strong) BXSAMTextView *labelTextView;
@property(nonatomic,strong)UIView *navigationView;
@property(nonatomic,strong)UIButton *uploadBtn;
@property(nonatomic, strong)UILabel *NumLabel;

@end

@implementation HPersonalizedSignatureVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    [self.view setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    [self setupNavigationView];
 
    
    [self createUI];
}
-(void)setupNavigationView{
    _navigationView = [UIView new];
    [self.view addSubview:_navigationView];
    [_navigationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(__kTopAddHeight+64);
    }];
    [_navigationView setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    WS(weakSelf);
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addTarget:self action:@selector(pop) forControlEvents:BtnTouchUpInside];
    [_navigationView addSubview:backBtn];
    [backBtn setImage:CImage(@"back_black") forState:BtnNormal];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.mas_equalTo(0);
        make.width.height.mas_equalTo(44);
    }];
    UILabel *label = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:NSLocalizedString(@"个性签名", nil) Font:SLBFont(__ScaleWidth(18)) TextColor:sl_textColors];
    label.textAlignment = 1;
    [_navigationView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(44);
        make.right.mas_equalTo(-44);
        make.centerY.equalTo(weakSelf.navigationView.mas_bottom).offset(-22);
        make.height.mas_equalTo(25);
    }];
    
    UIButton *uploadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_navigationView addSubview:uploadBtn];
    [uploadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.navigationView.mas_bottom).offset(-22);
        make.right.mas_equalTo(__ScaleWidth(-12));
        make.height.mas_equalTo(__ScaleWidth(26));
        make.width.mas_equalTo(__ScaleWidth(52));
    }];
    [uploadBtn.layer setMasksToBounds:YES];
    uploadBtn.layer.cornerRadius = __ScaleWidth(13);
    [uploadBtn setTitle:NSLocalizedString(@"保存", nil) forState:BtnNormal];
    [uploadBtn setTitleColor:sl_whiteTextColors forState:BtnNormal];
    [uploadBtn setBackgroundColor:sl_normalColors];
    uploadBtn.titleLabel.font = SLPFFont(__ScaleWidth(14));
    [uploadBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:BtnTouchUpInside];
    self.uploadBtn = uploadBtn;
}


- (void)createUI {
    UIView *backgroudView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    backgroudView.backgroundColor =sl_subBGColors;
    [self.view addSubview:backgroudView];
    WS(weakSelf);
    [backgroudView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.navigationView.mas_bottom).offset(__ScaleWidth(11));
        make.left.mas_equalTo(__ScaleWidth(12));
        make.right.mas_equalTo(__ScaleWidth(-12));
        make.height.mas_equalTo(__ScaleWidth(85));
    }];
    backgroudView.layer.masksToBounds = YES;
    backgroudView.layer.cornerRadius = 5;
    
    NSAttributedString *attri = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"请输入你的个性签名", nil) attributes:@{NSForegroundColorAttributeName:sl_textSubColors, NSFontAttributeName: SLPFFont(__ScaleWidth(14))}];
    
    self.labelTextView = [[BXSAMTextView alloc]initWithFrame:CGRectMake(12, 10, SCREEN_WIDTH-24, 180)];
    self.labelTextView.backgroundColor = [UIColor clearColor];
    self.labelTextView.delegate = self;
    self.labelTextView.textColor = sl_textColors;
    self.labelTextView.textAlignment=NSTextAlignmentLeft;
    self.labelTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//高度自适应
    self.labelTextView.returnKeyType = UIReturnKeyDone;//返回键类型
    self.labelTextView.scrollEnabled = YES;//是否可以拖动
    self.labelTextView.font = SLPFFont(__ScaleWidth(14));
    self.labelTextView.showsVerticalScrollIndicator = NO;
    self.labelTextView.attributedPlaceholder = attri;
    [backgroudView addSubview:self.labelTextView];
    [self.labelTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(__ScaleWidth(15));
        make.bottom.right.mas_equalTo(__ScaleWidth(-15));
    }];
    if (!IsNilString(self.sign)) {
        self.labelTextView.text = self.sign;
    }
    [self.labelTextView becomeFirstResponder];
    
    _NumLabel = [UILabel new];
    _NumLabel.textColor = sl_textSubColors;
    _NumLabel.font = SLPFFont(12);
    _NumLabel.text = [NSString stringWithFormat:@"%lu/30",(unsigned long)self.labelTextView.text.length] ;
    _NumLabel.textAlignment = 2;
    [self.view addSubview:_NumLabel];
    [_NumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(backgroudView.mas_bottom).offset(5);
        make.right.mas_equalTo(backgroudView.mas_right);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(10);
    }];
}

#pragma mark - 保存
- (void)saveBtnClick:(UIButton *)btn
{
    [self.labelTextView resignFirstResponder];
    if (self.labelTextView.text.length<2||self.labelTextView.text.length>30) {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"个性签名2~30个字符", nil)];
    } else {
        [[NewHttpRequestHuang sharedNewHttpRequestHuang] userSaveInfoWithNickName:@"" Gender:@"" Birthday:@"" Province_id:@"" City_id:@"" District_id:@"" Sign:self.labelTextView.text Fields:@"sign" Success:^(id responseObject) {
            if ([responseObject[@"code"] intValue]==0) {
                BXLiveUser *liveUser = [BXLiveUser currentBXLiveUser];
                liveUser.sign = self.labelTextView.text;
                [BXLiveUser setCurrentBXLiveUser:liveUser];
                
                if (self.didUpdateSign) {
                    self.didUpdateSign(self.labelTextView.text);
                }
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [BGProgressHUD showInfoWithMessage:responseObject[@"msg"]];
            }
        } Failure:^(NSError *error) {
            [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"修改失败", nil)];
        }];
    }
}

#pragma mark - 隐藏键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.labelTextView resignFirstResponder];
}

#pragma mark - UITextViewDelegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        
        return NO;
    }
    
    NSString *lastStr = [textView.text stringByReplacingCharactersInRange:range withString:text];
    
    if (lastStr.length  == 0) {
//        空字符串
        [self.uploadBtn setTitleColor:sl_textSubColors forState:BtnNormal];
        [self.uploadBtn setBackgroundColor:sl_subBGColors];
    }else{
        [self.uploadBtn setTitleColor:sl_whiteTextColors forState:BtnNormal];
        [self.uploadBtn setBackgroundColor:sl_normalColors];
    }
    
    return YES;
}
-(void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length > 30)
      {
          textView.text = [textView.text substringToIndex: 30];
      }
    _NumLabel.text = [NSString stringWithFormat:@"%lu/30",(unsigned long)textView.text.length] ;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
